<template>
<div class="user">
    <div class="common-seach-wrap">
      <el-form
        size="small"
        :inline="true"
        :model="searchForm"
        class="demo-form-inline"
      >
        <el-form-item :label="$t('plus.form_selection')">
          <el-select size="small" v-model="searchForm.tableId" :placeholder="$t('plus.all_forms')">
            <el-option :label="$t('plus.all')" :value="0"></el-option>
            <el-option v-for="(item, index) in tableList" :key="index" :label="item.name"
                       :value="item.tableId"></el-option>
          </el-select>
        </el-form-item>

        <el-form-item :label="$t('plus.member_info')">
          <el-input v-model="searchForm.search" :placeholder="$t('plus.input_nickname_id')"></el-input>
        </el-form-item>
        <el-form-item>
          <el-button size="small" type="primary" icon="Search" @click="onSubmit"
            >{{$t('plus.search')}}</el-button
          >
        </el-form-item>
        <el-form-item
          ><el-button size="small" type="success" @click="onExport"
            >{{$t('plus.export')}}</el-button
          ></el-form-item
        >
      </el-form>
    </div>
    <!--内容-->
    <div class="product-content">
      <div class="table-wrap">
        <el-table
          size="small"
          :data="tableData"
          border
          style="width: 100%"
          v-loading="loading"
        >
          <el-table-column prop="name" :label="$t('plus.form_name')"></el-table-column>
          <el-table-column prop="userId" :label="$t('plus.user_id')"></el-table-column>
          <el-table-column prop="nickName" :label="$t('plus.user_nickname')"></el-table-column>
          <el-table-column prop="createTime" :label="$t('plus.create_time')"></el-table-column>
          <el-table-column fixed="right" :label="$t('plus.action')" width="90">
            <template #default="scope">
              <el-button
                @click="detailClick(scope.row)"
                type="text"
                size="small"
                >{{$t('plus.details')}}</el-button
              >
              <el-button
                @click="deleteClick(scope.row)"
                type="text"
                size="small"
                v-auth="'/plus/table/record/delete'"
                >{{$t('plus.delete')}}</el-button
              >
            </template>
          </el-table-column>
        </el-table>
      </div>

      <!--分页-->
      <div class="pagination">
        <el-pagination
          @size-change="handleSizeChange"
          @current-change="handleCurrentChange"
          background
          :current-page="curPage"
          :page-size="pageSize"
          layout="total, prev, pager, next, jumper"
          :total="totalDataNumber"
        ></el-pagination>
      </div>
    </div>
    <el-dialog :title="$t('plus.form_detail')" v-model="dialogDetail" width="50%">
      <el-form size="small">
        <el-form-item :label="$t('plus.form_name_label')">
          <span class="gray">{{currentModel.name}}</span>
        </el-form-item>
        <el-form-item :label="$t('plus.user_info_label')">
          <span class="gray"
            >{{currentModel.nickName}}({{currentModel.userId}})</span
          >
        </el-form-item>
        <el-form-item
          :label="item.name + '：'"
          v-for="(item, index) in currentModel.fields"
          :key="index"
        >
          <span class="gray">{{item.value}}</span>
        </el-form-item>
      </el-form>
    </el-dialog>
  </div>
</template>

<script>
import TableApi from "@/api/plus/table.js";
import { getCookie } from "@/utils/base.js";
import qs from "qs";
import { useUserStore } from "@/store";
const {
  token
} = useUserStore();
export default {
  data() {
    return {
      token,
      /*是否加载完成*/
      loading: false,
      /*列表数据*/
      fields: [],
      /*一页多少条*/
      pageSize: 15,
      /*一共多少条数据*/
      totalDataNumber: 0,
      /*当前是第几页*/
      curPage: 1,
      searchForm: {
        tableId: 0,
        search: ''
      },
      dialogDetail: false,
      currentModel: {
        tableM: {},
        user: {}
      },
      tableList: []
    };
  },
  created() {
    /*获取列表*/
    this.getTableList();
  },
  methods: {
    onExport: function () {
      let self = this;
      let search = this.searchForm;
      let Params = {};
      Params.tableId = self.searchForm.tableId;
      Params.search = self.searchForm.search;
      if (Params.tableId <= 0) {
        ElMessage({
          message: $t("plus.select_form"),
          type: "error"
        });
        return;
      }
      Params.tokenshop = self.token;
      let baseUrl = import.meta.env.VITE_BASIC_URL;
      if (!baseUrl) {
        baseUrl = window.location.protocol + "//" + window.location.host;
      }
      window.location.href = baseUrl + "/api/shop/plus/table/record/export?" + qs.stringify(Params);
    },
    /*选择第几页*/
    handleCurrentChange(val) {
      let self = this;
      self.curPage = val;
      self.loading = true;
      self.getTableList();
    },
    /*每页多少条*/
    handleSizeChange(val) {
      this.curPage = 1;
      this.pageSize = val;
      this.getTableList();
    },
    /*获取列表*/
    getTableList() {
      let self = this;
      let Params = self.searchForm;
      Params.pageIndex = self.curPage;
      Params.pageSize = self.pageSize;
      self.loading = true;
      TableApi.getRecordList(Params, true).then(res => {
        self.loading = false;
        self.tableData = res.data.list.records;
        self.totalDataNumber = res.data.list.total;
        self.tableList = res.data.tableList;
      }).catch(error => {});
    },
    onSubmit() {
      this.curPage = 1;
      this.getTableList();
    },
    /*打开编辑*/
    detailClick(row) {
      this.currentModel = row;
      console.log(this.currentModel);
      this.dialogDetail = true;
    },
    /*删除*/
    deleteClick(row) {
      let self = this;
      ElMessageBox.confirm($t("plus.delete_confirm"), $t("plus.prompt"), {
        confirmButtonText: $t("plus.confirm_text"),
        cancelButtonText: $t("plus.cancel_text"),
        type: "warning"
      }).then(() => {
        self.loading = true;
        TableApi.delRecord({
          tableId: row.tableRecordId
        }, true).then(data => {
          self.loading = false;
          if (data.code == 1) {
            ElMessage({
              message: $t("plus.delete_success_tip"),
              type: "success"
            });
            //刷新页面
            self.getTableList();
          } else {
            self.loading = false;
          }
        }).catch(error => {
          self.loading = false;
        });
      }).catch(() => {});
    }
  }
};
</script>

